Some tests and good practice of Numpy

1. Why numpy? Python is "slow"

The simple reason is that Python a dynamically typed, interpreted language, where values are stored not in dense buffers but in scattered objects. So every thing in python is an object, even premitive types such as int or float. The objects in a list un-orderly stored in the memory... So it is reasonable that Python is slow. However, Pyhon is fast in terms of pototyping everything. In addtion, Python can easily call other compiled-type library to boost up! Numpy is one of the choice to boost up the code specified for matrix calculation. In this case, you get both fast developing and speed.

So, why Numpy is fast? Or why the array in numpy is faster than python list object? Here is a good explaination from reference 2.

References

  1. Getting the Best Performance out of NumPy
  2. Why Python is slow?

2. Use numpy the right way

2.1 C type or F type?

C-type n-demension array is row based, while F-type is column based, as this figure shows:

Therefore, if we do more based on row, it is better to use C-type array. Following code gives the performance:


In [19]:
import numpy as np

a = np.zeros((2000, 2000), order='C')
b = np.zeros((2000, 2000), order='F')

def f1(a):
    np.concatenate((a, a), axis=0)  # on row

def f2(b):
    np.concatenate((b, b), axis=0)  # on row

%timeit f1(a)
%timeit f2(b)


479 µs ± 4.33 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
766 µs ± 19 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)